﻿/***************************************************************
 * au  :zhuyehong
 * time:2011.9
 ROLL_TABS begin
 ****************************************************************/
function ROLL_TABS(myname, menuID, floatmenu) {
  var tabs = {};
  tabs.title = [];
  tabs.child_title = [];
  tabs.title_html = "";
  tabs.menuPos = "";
  tabs.tabs_selector = 0;
  tabs.values = [];
  tabs.actions = [];
  tabs.myname = "";

  tabs.left_status = 0;
  tabs.right_status = 1;
  tabs.display = 0;

  tabs.icon = {
    left_off: '',
    right_off: '',
    list_off: '',
    left_on: '',
    right_on: '',
    list_on: '',
    left_disable: '',
    right_disable: ''
  };

  tabs.menu_begin = 0;
  tabs.menu_end = 0;

  tabs.response = "";

  tabs.PRINT_TITLE = function (i) {
    alert(tabs.title_html);
  };

  tabs.FIND_TITLE_INDEX = function (titleName) {
    var i = 0, len = 0;
    for (i = 0, len = tabs.title.length; i < len; i += 1) {
      if (titleName === tabs.title[i]) {
        return i;
      }
    }

    return -1;
  };

  tabs.GET_MENU_ATTR = function () {
    var attr = new Object();

    var width = $(tabs.menuPos).css('width');
    var font_size = $(tabs.menuPos).css('font-size');

    attr.width = parseInt(width.substring(0, width.length - 2));
    attr.font_size = parseInt(font_size.substring(0, width.length - 2));

    return attr;
  };

  tabs.GET_CAPACITY = function (begin) {
    var attr = tabs.GET_MENU_ATTR();
    var total = tabs.title.length;
    var ret = new Object();

    var width = attr.width;
    var font_size = attr.font_size;

    var i = begin;
    var count = 78;
    var reg = /[\u4E00-\u9FA5\uf900-\ufa2d]/g;
    var pre_count = count;

    for (; i < tabs.title.length; i++) {
      var eg = tabs.title[i].replace(reg, '');

      pre_count = count;
      if (i == tabs.tabs_selector) {
        count = count + (tabs.title[i].length - eg.length) * (font_size + 1) + parseInt(eg.length * (font_size + 1) / 2) + 3 + 8 + tabs.title[i].length;
      } else {
        count = count + (tabs.title[i].length - eg.length) * font_size + eg.length * font_size / 2 + 3 + 8 + tabs.title[i].length;
      }

      if (count > width) {
        count = pre_count;
        i = i - 1;
        break;
      }
    }

    ret.count = i;

    if (++i < tabs.title.length && (width - count) > 0) {
      if (i == tabs.tabs_selector) {
        ret.left = 0;
        var leftPixel = width - count - 12 - 3 - 5;
        for (var k = 0; leftPixel > 0 && k < tabs.title[i].length; k++) {
          if (tabs.title[i].charAt(k).match(reg)) {
            leftPixel = leftPixel - font_size - 1;
          } else {
            leftPixel = leftPixel - (font_size / 2) - 1;
          }
          ret.left++;
        }
        //ret.left = ret.left>1? (ret.left-1):0;
        ret.context = tabs.title[i].substring(0, ret.left - 1);
      } else {
        ret.left = 0;
        var leftPixel = width - count - 12 - 3 - 5;
        for (var k = 0; leftPixel > 0 && k < tabs.title[i].length; k++) {
          if (tabs.title[i].charAt(k).match(reg)) {
            leftPixel = leftPixel - font_size - 1;
          } else {
            leftPixel = leftPixel - font_size / 2 - 1;
          }
          ret.left++;
        }
        //ret.left = ret.left>1? (ret.left-1):0;
        ret.context = tabs.title[i].substring(0, ret.left - 1);
        //alert(ret.context);
      }
    } else {
      ret.left = 0;
    }
    //alert(ret.count + ":" + begin);
    return ret;
  };

  tabs.ROLL_LEFT = function (node) {
    if (tabs.menu_begin > 0) {
      tabs.menu_begin--;
      tabs.SHOW_TABS(tabs.tabs_selector);
      $(tabs.menuPos).empty();
      $(tabs.menuPos).append(tabs.title_html);
    }

  };

  tabs.ROLL_RIGHT = function (node) {
    if (tabs.menu_end < (tabs.title.length - 1)) {
      tabs.menu_begin++;
      tabs.SHOW_TABS(tabs.tabs_selector);
      $(tabs.menuPos).empty();
      $(tabs.menuPos).append(tabs.title_html);
    }

  };

  tabs.DOWN = function (node, type) {

  };
  tabs.menu_begin = 0;
  tabs.menu_end = 0;
  tabs.ROLL_OVER = function (img, type) {

  };

  tabs.ROLL_OUT = function (img, type) {

  };

  tabs.TAB_OVER = function (node) {
    if (!$(node).hasClass("selected")) {
      $(node).addClass("hover");
    }
  };

  tabs.TAB_OUT = function (node) {
    if (!$(node).hasClass("selected")) {
      $(node).removeClass("hover");
    }
  };

  tabs.node = '';
  tabs.LIST = function (node) {
    tabs.node = node;
    if (tabs.display == 1)
      return;
    setTimeout(tabs.LIST_IN, 300);
  };

  tabs.LIST_IN = function () {
    node = tabs.node;
    var offset = $(node).offset();
    var left = offset.left;
    var top = offset.top;
    var maxLength = 0;

    $(tabs.floatmenu).empty();
    //$(tabs.floatmenu).append("<ul>");
    var html = ""
    var maxWidth = 0;
    var font_size = parseInt($("#floatmenu").css("font-size"));

    var liWidth = '100%';
    /*
     if(jQuery.browser.msie)
     {
     var version = jQuery.browser.version;
     if(version=='8.0')
     {
     liWidth = '100%';
     }
     }
     else
     {
     liWidth = '100%';
     }*/

    for (var i = 0; i < tabs.title.length; i++) {
      if (tabs.tabs_selector == i) {
        var prewidth = tabs.title[i].length * (font_size + 1);
        maxWidth = maxWidth >= prewidth ? maxWidth : prewidth;
      } else {
        var prewidth = tabs.title[i].length * font_size;
        maxWidth = maxWidth >= prewidth ? maxWidth : prewidth;
      }
    }

    for (var i = 0; i < tabs.title.length; i++) {
      if (tabs.tabs_selector == i) {
        html = html + "<li class='selected' style='width:" + maxWidth + "px' onMouseDown='" + tabs.myname + ".LIST_SELECTED(" + i + ")' onMouseover='" + tabs.myname + ".LIST_OVER(this," + i + ")' onMouseout='" + tabs.myname + ".LIST_OUT(this," + i + ")'>" + tabs.title[i] + "</li>";
      } else {
        html = html + "<li style='width:" + maxWidth + "px' onMouseDown='" + tabs.myname + ".LIST_SELECTED(" + i + ")' onMouseover='" + tabs.myname + ".LIST_OVER(this," + i + ")' onMouseout='" + tabs.myname + ".LIST_OUT(this," + i + ")'>" + tabs.title[i] + "</li>";
      }
      maxLength = maxLength > tabs.title[i].length ? maxLength : tabs.title[i].length;
    }

    html = html + "<a id= '" + tabs.myname + "_list' href='#' style='height:3px;' onBlur='" + tabs.myname + ".LIST_BLUR()'></a>";
    html = "<ul style='width:" + maxWidth + "px'>" + html + "</ul>";

    $(tabs.floatmenu).append(html);
    //$(tabs.floatmenu).append("</ul>");
    //$(tabs.floatmenu).append("<a id= '" + tabs.myname + "_list' href='#' style='height:3px;' onBlur='" + tabs.myname +".LIST_BLUR()' ></a>");

    $(tabs.floatmenu).show();
    $('#' + tabs.myname + '_list').focus();

    setTimeout(tabs.LIST_FOCUS, 50);
  };

  tabs.LIST_BLUR = function () {
    setTimeout(tabs.HIDE_LIST_DO, 200);
  };

  tabs.LIST_FOCUS = function () {
    $('#' + tabs.myname + '_list')[0].focus();
    tabs.display = 1;
  };

  tabs.HIDE_LIST_DO = function () {
    $(tabs.floatmenu).hide();
    tabs.display = 0;
  };

  tabs.LIST_OVER = function (node, i) {
    //$(node).css('background','yellow');
    $(node).addClass("hover");
  };

  tabs.LIST_OUT = function (node, i) {
    if (tabs.tabs_selector == i) {
      //$(node).css('background','gray');
    } else {
      //$(node).css('background','#c9f5f6');
      $(node).removeClass("hover");
    }
  };

  tabs.LIST_SELECTED = function (selector) {
    tabs.menu_begin = 0;
    var ret = tabs.GET_CAPACITY(tabs.menu_begin);

    for (; tabs.menu_begin < tabs.title.length;) {
      if (selector > ret.count) {
        tabs.menu_begin++;
        ret = tabs.GET_CAPACITY(tabs.menu_begin);
      } else {
        break;
      }
    }

    tabs.SHOW_TABS(selector);
    if (typeof tabs.actions[selector] == 'function') {
      tabs.actions[selector](tabs.values[selector]);
    } else {
      try {
        tabs.actions(tabs.values[selector]);
      } catch (e) {
        alert("Title callback need a function！");
      }

    }

  };

  tabs.SET_TITLE = function (title, selector, values, action) {
    selector = selector ? selector : 0;
    tabs.tabs_selector = selector;

    if (1 <= title.length) {
      tabs.title = title;

      tabs.values = values;
      tabs.actions = action;

      var ret = tabs.GET_CAPACITY(tabs.menu_begin);

      var title_end = ret.count;
      tabs.menu_end = title_end;

      var i = tabs.menu_begin;
      tabs.title_html = "<table id='" + tabs.myname + "_menu' nowarp='nowarp' cellspacing='0' cellpadding='0' width='100%'>";
      tabs.title_html = tabs.title_html + "<tr><td class='tabTd'>";

      for (i = tabs.menu_begin; i < title.length && i <= title_end; i++) {
        if (i == selector) {
          tabs.title_html = tabs.title_html + "<div nowarp='nowarp' style='cursor:pointer;' id ='" + tabs.myname + "_selected' class='selected' onMouseOver='" + tabs.myname + ".TAB_OVER(this)' onMouseOut='" + tabs.myname + ".TAB_OUT()' onclick='OPEN(" + tabs.myname + "," + i + ")'>" + title[i] + "</div>";
        } else {
          tabs.title_html = tabs.title_html + "<div nowarp='nowarp' style='cursor:pointer;' onMouseOver='" + tabs.myname + ".TAB_OVER(this)' onMouseOut='" + tabs.myname + ".TAB_OUT(this)'  onclick='OPEN(" + tabs.myname + "," + i + ")'>" + title[i] + "</div>";
        }
      }

      if (ret.left > 0) {
        if (i == tabs.tabs_selector) {
          tabs.title_html = tabs.title_html + "<div nowarp='nowarp' style='cursor:pointer;' class='selected' onMouseOver='" + tabs.myname + ".TAB_OVER(this)' onMouseOut='" + tabs.myname + ".TAB_OUT(this)'  onclick='OPEN(" + tabs.myname + "," + i + ")'>" + ret.context + "</div>";
        } else {
          tabs.title_html = tabs.title_html + "<div nowarp='nowarp' style='cursor:pointer;' onMouseOver='" + tabs.myname + ".TAB_OVER(this)' onMouseOut='" + tabs.myname + ".TAB_OUT(this)'  onclick='OPEN(" + tabs.myname + "," + i + ")'>" + ret.context + "</div>";
        }
      }

      tabs.title_html = tabs.title_html + "</td>";

      if (title_end >= (tabs.title.length - 1) && tabs.menu_begin == 0) {
      } else {
        if (tabs.menu_end == tabs.title.length || tabs.menu_begin == 0) {
          if (tabs.menu_end == tabs.title.length) {
            tabs.title_html = tabs.title_html + "<td  width='60' height='27' class='tabControl'><a href='#' class='gotoL' id='" + tabs.myname + "_gotoL' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,1)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,1)' onMouseDown='" + tabs.myname + ".DOWN(this,1)' onMouseUp='" + tabs.myname + ".ROLL_LEFT(this)' ></a> <a href='#' class='gotoR_disable' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,2)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,2)' onMouseDown='" + tabs.myname + ".DOWN(this,2)'  onMouseUp='" + tabs.myname + ".ROLL_RIGHT(this)' ></a> <a href='#' class='gotoD' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,3)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,3)' onMouseDown='" + tabs.myname + ".DOWN(this,3)' onclick='" + tabs.myname + ".LIST(this)' ondbclick='' ></a></td>";
          }
          if (tabs.menu_begin == 0) {
            tabs.title_html = tabs.title_html + "<td width='60 'height='27' class='tabControl'><a href='#' class='gotoL_disable' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,1)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,1)' onMouseDown='" + tabs.myname + ".DOWN(this,1)' onMouseUp='" + tabs.myname + ".ROLL_LEFT(this)' ></a> <a href='#' class='gotoR' id='" + tabs.myname + "_gotoR' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,2)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,2)' onMouseDown='" + tabs.myname + ".DOWN(this,2)'  onMouseUp='" + tabs.myname + ".ROLL_RIGHT(this)' ></a> <a href='#' class='gotoD' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,3)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,3)' onMouseDown='" + tabs.myname + ".DOWN(this,3)' onclick='" + tabs.myname + ".LIST(this)' ondbclick='' ></a></td>";
          }
        } else {
          tabs.title_html = tabs.title_html + "<td width='60' 'height='27' class='tabControl'><a href='#' class='gotoL' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,1)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,1)' onMouseDown='" + tabs.myname + ".DOWN(this,1)' onMouseUp='" + tabs.myname + ".ROLL_LEFT(this)' ></a> <a href='#' class='gotoR' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,2)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,2)' onMouseDown='" + tabs.myname + ".DOWN(this,2)'  onMouseUp='" + tabs.myname + ".ROLL_RIGHT(this)' ></a> <a href='#' class='gotoD' onMouseover = '" + tabs.myname + ".ROLL_OVER(this,3)' onMouseout='" + tabs.myname + ".ROLL_OUT(this,3)' onMouseDown='" + tabs.myname + ".DOWN(this,3)' onclick='" + tabs.myname + ".LIST(this)' ondbclick='' ></a></td>";
        }
      }
      tabs.title_html = tabs.title_html + "</tr></table>";
    }
  };

  tabs.SHOW_TABS = function (i) {
    tabs.SET_TITLE(tabs.title, i, tabs.values, tabs.actions);
    tabs.tabs_selector = i;
    $(tabs.menuPos).empty();
    $(tabs.menuPos).append(tabs.title_html);
  };

  tabs.SHOW_TABLE = function () {
    var id = "#" + tabs.myname + "_selected";
    $(id).click();
  };

  tabs.RESIZE = function () {
    $('#' + tabs.myname + '_list').blur();
    tabs.SHOW_TABS(tabs.tabs_selector);
  };

  tabs.INIT = function () {
    tabs.myname = myname;
    tabs.menuPos = "#" + menuID;
    if (floatmenu) {
      tabs.floatmenu = "#" + floatmenu;
      $(tabs.floatmenu).hide();
    }
  };

  tabs.INIT();
  return tabs;
}

function OPEN(myname, tabId) {
  myname.SHOW_TABS(tabId);
  if (typeof myname.actions[tabId] == 'function') {
    myname.actions[tabId](myname.values[tabId]);
  }
  else {
    try {
      myname.actions(myname.values[tabId]);
    }
    catch (e) {
      alert("Title callback need a function！");
    }
  }
}